/**
 * Advanced Software Engineering - assignment part 1 (monica Farrow)
 * CustomerQueue class
 * 
 * Some of the methods in this class have been based on lecture notes provided by
 * Monica Farrow as a part of Software Engineering courses at Heriot-Watt University.
 */

package depot;

import java.util.LinkedList;
import java.util.NoSuchElementException;

/**
 * Advanced Software Engineering - F21AS.
 * CustomerQueue class
 * @author Ionut Iordache, Nikky Afunugo, Dave Bond
 */
public class CustomerQueue {

	private LinkedList<Customer> custQueue; 

	/** 
	 * constructor for CustomerQueue
	 * creates an empty LinkedList for object type Customer
	 */ 
	public CustomerQueue() { 
		custQueue = new LinkedList<Customer>(); 
	} 

	
	/** 
	 * takes ID and iterates through custQueue
	 * then returns the Customer object with matching ID
	 * @param id	the ID of the customer
	 * @return the customer or null
	 */
	public Customer getCustomerByID(String id) {
		for (Customer cust : custQueue) {
			if (cust.getCustID().equalsIgnoreCase(id))
			{
				return cust;
			}
		}
		return null;
	}

	
	/** 
	 * removes a Customer from custQueue
	 * @return	Boolean indicating success or failure
	 */ 
	public Boolean removeFromQueue(Customer cust) { 
		Boolean custRemoved = false;
		custRemoved = custQueue.remove(cust);
		return custRemoved;
	} 


	/** 
	 * obtains the first Customer in custQueue
	 * @return	the Customer or null
	 */ 
	public Customer getFirst() { 
		Customer cust = null;
		try{
			cust = custQueue.getFirst();
		} catch (NoSuchElementException nse) {
			cust = null;
			return cust;
		}
		return cust;
	} 

	
	/**
	 * Processes each line from processInputFIle, extracts data,
	 * creates Customer object and adds to custQueue.
	 * Each line contains: ID, first name, last name, parcelID (not used)
	 * @param line the line to be processed
	 * @throws IllegalArgumentException, DuplicateIDException, ArrayIndexOutOfBoundsException 
	 */
	public void processLine(Parcel pcl, String line, int lineNum) throws IllegalArgumentException, DuplicateIDException, ArrayIndexOutOfBoundsException {

		String details [] = line.split(",");

		// take id
		String id = details[0];
		id = id.trim();

		// checks custQueue for duplicate ID
		Customer cust99 = getCustomerByID(id);
		if(cust99 != null) {
			throw new DuplicateIDException("CustomerID "+id+" already exists in customer queue");
		}

		// take first name
		String fName = details[1];

		// take last name
		String lName = details[2];
		
		// take parcelID
		String pID = details[3];

		// creates new Customer object and adds to custQueue
		Customer cust = new Customer(id, fName, lName, pcl);
		custQueue.addLast(cust);

	}

}
